package com.example.whm.loader;

import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.content.AsyncTaskLoader;

import com.example.whm.lib.client;
import com.example.whm.model.Item;

public class  asytask extends AsyncTaskLoader<List<Item>>{

	Context c;
	Bundle b;
	List<Item> list; 
	client client=new client();
	public asytask(Context context,Bundle bundle) {
		super(context);
		c=context;
		b=bundle;
		// TODO Auto-generated constructor stub
	}


	
	public asytask(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}




	@Override
	public List<Item> loadInBackground() {
		// TODO Auto-generated method stub
		System.out.println("loadInBackground pos="+b.getInt("pos"));
		//list=new ArrayList<Item>();
		List<Item> data=new ArrayList<Item>();
		String url=b.getString("url");
		int pos=b.getInt("pos");
		int start=pos*10;
		int end=start+10;
		list=client.get(Item.class, url);
		//Collections.sort(list);
		int size=list.size();
		if (start>=size) {
			data=new ArrayList<Item>();
		}
		else if (end>size) {
			data=list.subList(start, size-1);
		}
		else{
			data=list.subList(start, end);
		}
		
		
		return data;
	}

	@Override
	public boolean cancelLoad() {
		// TODO Auto-generated method stub
	//	System.out.println("cancelLoad");
		return super.cancelLoad();
	}

	@Override
	public void dump(String prefix, FileDescriptor fd, PrintWriter writer,
			String[] args) {
		// TODO Auto-generated method stub
	//	System.out.println("dump");
		
		super.dump(prefix, fd, writer, args);
	}

	@Override
	public void onCanceled(List<Item> data) {
		// TODO Auto-generated method stub
	//	System.out.println("onCanceled");
		
		super.onCanceled(data);
	}

	@Override
	protected void onForceLoad() {
		// TODO Auto-generated method stub
	//	System.out.println("onForceLoad");
		
		super.onForceLoad();
	}

	@Override
	protected List<Item> onLoadInBackground() {
		// TODO Auto-generated method stub
	//	System.out.println("onLoadInBackground");
		
		return super.onLoadInBackground();
	}

	@Override
	public void setUpdateThrottle(long delayMS) {
		// TODO Auto-generated method stub
	//	System.out.println("setUpdateThrottle");
		
		super.setUpdateThrottle(delayMS);
	}

	@Override
	public void waitForLoader() {
		// TODO Auto-generated method stub
	//	System.out.println("waitForLoader");
		super.waitForLoader();
	}

	@Override
	public void deliverResult(List<Item> data) {
		// TODO Auto-generated method stub
	//	System.out.println("deliverResult");
		super.deliverResult(data);
	}

	@Override
	protected void onReset() {
		// TODO Auto-generated method stub
	//	System.out.println("onReset");
		super.onReset();
	}

	@Override
	protected void onStartLoading() {
		// TODO Auto-generated method stub
		System.out.println("onStartLoading pos="+b.getInt("pos"));
		if (list==null) {
			System.out.println("onStartLoading");
			forceLoad();
	}
		 
	}

	@Override
	protected void onStopLoading() {
		// TODO Auto-generated method stub
		//System.out.println("onStopLoading");
		super.onStopLoading();
	}
	
	
	
	
	
	
	
}
